package net.bsayiner.foreignExchange.Activities;

import java.util.ArrayList;

import net.bsayiner.foreignExchange.R;
import net.bsayiner.foreignExchange.Utilities.Currency;
import net.bsayiner.foreignExchange.Utilities.CurrencyReaderFromMarket;
import net.bsayiner.foreignExchange.Utilities.CurrencyReaderFromTcmb;
import net.bsayiner.foreignExchange.Utilities.Metal;
import net.bsayiner.foreignExchange.Utilities.MetalReader;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements Runnable {

	private CurrencyReaderFromTcmb currencyReaderFromTcmb;
	private CurrencyReaderFromMarket currencyReaderFromMarket;
	private MetalReader metalReader;
	private ArrayList<Currency> currenciesTcmb;
	private ArrayList<Currency> currenciesMarket;
	private ArrayList<Metal> metals;
	private ProgressDialog currencyUpdateDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!checkInternetConnection()) {
			final AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("");
			alert.setMessage("Programın düzgün olarak çalışması için internet bağlantısı gerekmektedir.");
			alert.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					System.exit(0);
				}
			});
			alert.show();
		} else {
			setContentView(R.layout.tabview);
			currencyReaderFromTcmb = new CurrencyReaderFromTcmb();
			currencyReaderFromMarket = new CurrencyReaderFromMarket();
			metalReader = new MetalReader();
			currencyUpdateDialog = ProgressDialog.show(this, "", "Kur bilgisi güncelleniyor...");
			Thread thread = new Thread(this);
			thread.start();
		}

	}

	private boolean checkInternetConnection() {
		ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			return false;
		}
	}

	public void run() {
		currenciesTcmb = currencyReaderFromTcmb.getCurrenciesFromWeb();
		metals = metalReader.getCurrenciesFromWeb();
		currenciesMarket = currencyReaderFromMarket.getCurrenciesFromWeb();
		handler.sendEmptyMessage(0);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			updateCurrencyData();
			currencyUpdateDialog.dismiss();
		}
	};

	private void updateCurrencyData() {
		Resources resources = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec tabSpec;
		Intent intent;
		intent = new Intent().setClass(this, TcmbCurrencyActivity.class);
		intent.putParcelableArrayListExtra("Currency", currenciesTcmb);
		tabSpec = tabHost.newTabSpec("").setIndicator("Tcmb", resources.getDrawable(R.drawable.tabselect_currency)).setContent(intent);
		tabHost.addTab(tabSpec);
		intent = new Intent().setClass(this, MarketCurrencyActivity.class);
		intent.putParcelableArrayListExtra("Currency", currenciesMarket);
		tabSpec = tabHost.newTabSpec("").setIndicator("Serbest", resources.getDrawable(R.drawable.tabselect_currency)).setContent(intent);
		tabHost.addTab(tabSpec);
		intent = new Intent().setClass(this, MetalActivity.class);
		intent.putParcelableArrayListExtra("Metal", metals);
		tabSpec = tabHost.newTabSpec("").setIndicator("Altın", resources.getDrawable(R.drawable.tabselect_metal)).setContent(intent);
		tabHost.addTab(tabSpec);
		intent = new Intent().setClass(this, CalculatorActivity.class);
		intent.putParcelableArrayListExtra("Currency", currenciesTcmb);
		intent.putParcelableArrayListExtra("CurrencyMarket", currenciesMarket);
		tabSpec = tabHost.newTabSpec("").setIndicator("Hesap", resources.getDrawable(R.drawable.tabselect_calculate)).setContent(intent);
		tabHost.addTab(tabSpec);
		tabHost.setCurrentTab(0);
	}
}